package com.wanda.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.Pay_TypeModel;
import com.wanda.model.ProjectModel;
import com.wanda.model.RoomPBUAPModel;
import com.wanda.model.Wd_Opening_ReportModel;
import com.wanda.model.Wd_Opening_ReportsModel;
import com.wanda.service.RoomService;
import com.wanda.service.Wd_Opening_ReportService;

import com.wanda.util.DateFormat;

@Controller
@RequestMapping(value = "/ht/report")
public class Wd_Opening_ReportController {

	@Autowired
	private Wd_Opening_ReportService wd_Opening_ReportService;

	@Autowired
	private RoomService roomService;

	// 添加开盘报告
	@RequestMapping(value = "/insert", produces = "text/html;charset=UTF-8")
	public @ResponseBody Map<String, Object> insertWd_Opening_Report(HttpSession session, HttpServletRequest request)
			throws Exception {

		System.err.println("开盘添加");
		Wd_Opening_ReportModel model = new Wd_Opening_ReportModel();
		String opening_report_id = UUID.randomUUID().toString();
		session.setAttribute("openingid", opening_report_id);
		model.setOpening_report_name(request.getParameter("opening_report_name"));
		model.setProject_id(request.getParameter("project_id"));
		model.setPay_type_id(request.getParameter("pay_type_id"));
		model.setUpdate_user(request.getParameter("update_user"));
		model.setUpdate_time(request.getParameter("update_time"));
		model.setDelete_user(request.getParameter("delete_user"));
		model.setDelete_time(request.getParameter("delete_time"));
		model.setDelete_state("0");
		model.setOpening_report_time(DateFormat.getNowDate());
		model.setRoom_id(request.getParameter("room_id"));
		model.setState(request.getParameter("state"));
		model.setPrice_type(request.getParameter("price_type"));
		model.setOpening_report_name(request.getParameter("opening_report_name"));
		model.setReport_type(request.getParameter("report_type"));
		model.setOpening_report_description(request.getParameter("opening_report_description"));
		model.setDingjin(request.getParameter("dingjin"));
		model.setXuke(request.getParameter("xuke"));
		model.setOpening_report_id(opening_report_id);
		request.getSession().setAttribute("price_type", model.getPrice_type());

		System.err.println("删除：" + model.getDelete_state());
		System.err.println("开盘名称：" + model.getOpening_report_name());
		System.err.println("项目id：" + model.getProject_id());
		System.err.println("房间状态：" + model.getState());
		System.err.println("支付：" + model.getPrice_type());
		System.err.println("开盘说明：" + model.getOpening_report_description());
		System.err.println(model.getOpening_report_time());
		System.err.println("定金：" + model.getDingjin());
		System.err.println("蓄客：" + model.getXuke());

		session.setAttribute("projectid", model.getProject_id());
		session.setAttribute("openingid", opening_report_id);
		int i = wd_Opening_ReportService.insertWd_Opening_Report(model);

		Map<String, Object> map = new HashMap<String, Object>();

		System.out.println("添加成功");
		map.put("msg", "成功");
		return map;
	}

	// 修改开盘报告
	@RequestMapping(value = "/update")
	public String updateWd_Opening_Report(Wd_Opening_ReportsModel model,HttpServletRequest request) {

		String ac=(String) request.getParameter("prices");
		model.setPrice_type(ac);
		model.setUpdate_time(DateFormat.getNowDate());
		wd_Opening_ReportService.updateWd_Opening_Report(model);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("openings", map);
		return "redirect:/ht/report/selectAll";
	}

	// 删除开盘报告
	@RequestMapping(value = "/delete")
	public @ResponseBody Map<String, Object> deleteWd_Opening_Report(String opening_report_id, String delete_user,
			String delete_time, String delete_state) {
		Wd_Opening_ReportModel model = new Wd_Opening_ReportModel();

		model.setOpening_report_id(opening_report_id);
		model.setDelete_user(delete_user);
		model.setDelete_state(delete_state);
		model.setDelete_time(DateFormat.getNowDate());

		int models = wd_Opening_ReportService.deleteWd_Opening_Report(model);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", models);
		System.out.println("删除成功");
		return map;
	}

	// 按照ID开盘报告
	@RequestMapping(value = "/selectById")
	public ModelAndView selectWd_Opening_ReportById(String id) {

		System.err.println("进入开盘单条查询");
		ModelAndView mav = new ModelAndView();
		Wd_Opening_ReportsModel model = null;
		try {
			model = wd_Opening_ReportService.selectById(id);
			System.err.println("bbbbb:" + model);
			mav.setViewName("/qt/updateOpening_Report.jsp");
			mav.addObject("openings", model);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mav;
	}

	// 查询所有开盘报告56
	@RequestMapping(value = "/selectAll")
	public ModelAndView selectAllWd_Opening_Report() {
		System.err.println("进人开盘查询所有");
		ModelAndView model = new ModelAndView();
		List<Wd_Opening_ReportsModel> opening = null;
		try {
			opening = wd_Opening_ReportService.selectAll();
			model.setViewName("/qt/listOpening_Report.jsp");
			// System.err.println("aaaaaa:"+opening.get(0).getProject().getProject_name());

			model.addObject("openings", opening);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}
	
	// 遍历开盘里的计价方式
	@RequestMapping(value = "/price")
	public @ResponseBody Map<String, Object> showAlls(String id) {

		Map<String, Object> map = new HashMap<String, Object>();

		System.err.println("进入开盘查询");
		List<Wd_Opening_ReportsModel> pros = null;
		pros = wd_Opening_ReportService.selectAll();

		map.put("msg", pros);
		return map;
	}
	
	// 查询所有开盘报告56
	@RequestMapping(value = "/selects")
	public ModelAndView selectAllWd_Opening_Reports() {
		System.err.println("进人开审核查询所有");
		ModelAndView model = new ModelAndView();
		List<Wd_Opening_ReportsModel> opening = null;
		try {
			opening = wd_Opening_ReportService.selectAll();
			model.setViewName("/qt/checkOpening_Report.jsp");

			model.addObject("openings", opening);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}
	
	// 开盘里的批量审核
	@RequestMapping(value = "/checks")
	public ModelAndView select(HttpServletRequest request,HttpSession session) {
		
		String[] str = request.getParameterValues("checkbox1");
		
		String projectid=(String) session.getAttribute("projectid");
		
		ModelAndView model1 = new ModelAndView();
		try {
			final List ids = new ArrayList();
			for (String st : str) {
				ids.add(st);
				System.err.println("//" + st);					
			}
			Map map = new HashMap();
			map.put("ids", ids);
            map.put("projectid", projectid);
			int s =wd_Opening_ReportService.updatess(map);

			int blogs = roomService.updatec(map);
		
			session.setAttribute("ids", map);
			
			model1.setViewName("/ht/report/selects");
			model1.addObject("rooms", blogs);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model1;
	}
	
	// 查询所有开盘报告
	@RequestMapping(value = "/selectAlls")
	public ModelAndView selectAllWd_Opening_Reports(HttpServletRequest request) {
		System.err.println("进人开盘查询所有");
		ModelAndView model = new ModelAndView();
		List<Wd_Opening_ReportsModel> opening = null;
		try {
			opening = wd_Opening_ReportService.selectAlls();
			System.err.println("aaaaaa:" + opening.get(0).getProjects().getProject_name());
			model.setViewName("/qt/yuLan.jsp");

			model.addObject("openings", opening);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	// 开盘预览
	@RequestMapping(value = "/updates")
	public @ResponseBody ModelAndView alls(HttpSession session,String payid) {

		System.err.println("预览开盘信息");
		String opid = (String) session.getAttribute("openingid");
		System.err.println(opid);
		
		ModelAndView model = new ModelAndView();
		Wd_Opening_ReportsModel opening = null;
		List<RoomPBUAPModel> opp=null;

		int abc = wd_Opening_ReportService.updates(payid, opid);//利用开盘id添加支付id信息
	    Map map = (Map) session.getAttribute("ids");
		System.err.println("开盘里的updates:" + map);
		
		map.put("opid", opid);		
		try {
			//查询开盘的一整条记录
			opening = wd_Opening_ReportService.selectById(opid);
			
			opp =roomService.selectsn(map); 
			
			session.setAttribute("aa", opp);
			session.setAttribute("opens", opening);
			model.setViewName("/qt/yuLan.jsp");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	/*
	 * @RequestMapping(value = "/qt/selectByOrganization") public @ResponseBody
	 * Map<String, Object> selectByOrganization(String Organization_id) {
	 * System.err.println("++++++++++++++++++++++++++" + "错了哈哈" +
	 * Organization_id); Map<String, Object> map = new HashMap<String,
	 * Object>(); List<Wd_Opening_ReportModel> Wd_Opening_ReportModelList =
	 * Wd_Opening_ReportService.selectByOrganization(Organization_id);
	 * 
	 * for (Wd_Opening_ReportModel d : Wd_Opening_ReportModelList) {
	 * System.err.println("用户的id是   " + d.getUser_id()); }
	 * 
	 * ModelAndView model = new ModelAndView();
	 * model.setViewName("qt/shouquan.jsp");
	 * model.addObject("Wd_Opening_ReportModelList",
	 * Wd_Opening_ReportModelList); map.put("result",
	 * Wd_Opening_ReportModelList); return map; }
	 */
}
